/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deti.es.persistence.bandprofile;

import deti.es.ukni.persistence.entity.*;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Implementation of the {@link BandProfileRemote} interface.
 * 
 * @author ukni
 */

@Stateless( mappedName="ejb/MyBandBandProfile" )
public class BandProfileBean implements BandProfileRemote {

    @PersistenceContext
    private EntityManager manager;


    public BandProfileBean(){}

    /**
    *  Implementation of (@link BandProfileRemote#createElement)
    */
    public void createElement(Band band, String name, String role, String bio) {

        Bandelement newElement = new Bandelement();

        newElement.setName(name);
        newElement.setRole(role);
        newElement.setBiography(bio);
        newElement.setRefidband(band);
        
        manager.persist(newElement);
        band.getBandelementCollection().add(newElement);
    }

    /**
    *  Implementation of (@link BandProfileRemote#updateElement)
    */
    public void updateElement(Bandelement element) {
        manager.merge(element);
    }

    /**
    *  Implementation of (@link BandProfileRemote#postVideo)
    */
    public void postVideo(Band band, String name, String desc, String embed) {

        Video video = new Video();
        video.setRefidband(band);
        video.setName(name);
        video.setDescription(desc);
        video.setEmbeddedcode(embed);
        video.setDatecreation(new java.util.Date());

        manager.persist(video);
        band.getVideoCollection().add(video);
        manager.merge(band);
    }

    /**
     * Implementation of {@link BandProfileRemote#postImage}
     */
    public void postImage(Band band, String name, String desc, String uri) {

        Image image = new Image();

        image.setRefidband(band);
        image.setName(name);
        image.setDescription(desc);
        image.setUri(uri);
        image.setDatecreation(new java.util.Date());

        manager.persist(image);
        band.getImageCollection().add(image);
        manager.merge(band);
    }

}